package com.xci.demo;

import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.BCUtil;
import cn.hutool.crypto.asymmetric.SM2;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;

public class Program {
    public static void main(String[] args) {
        // String text = "_测_fasde-2023@试_";
        // System.out.println("原始数据: " + text);
        //
        // // SM3 Hash
        // String sm3Hash = SmCryptoUtils.sm3Hash(text);
        // System.out.println("sm3Hash: " + sm3Hash);
        // System.out.println("------------------------------------------------");
        //
        // // SM4 加密解密
        // String key = SmCryptoUtils.sm4GenerateKey();
        // System.out.println("key: " + key);
        // String iv = SmCryptoUtils.sm4GenerateKey();
        // System.out.println("iv: " + iv);
        //
        // String encryptData = SmCryptoUtils.sm4Encrypt(text, key, iv); // CBC模式
        // System.out.println("encryptData_原始Hex: " + encryptData);
        //
        // String decryptData = SmCryptoUtils.sm4Decrypt(encryptData, key, iv); // CBC模式
        // System.out.println("sm4Decrypt: " + decryptData);
        // System.out.println("------------------------------------------------");
        //
        // // SM2 密钥对生成
        // var keypair = SmCryptoUtils.sm2GenerateKeyPairHex();
        // // String publicKey = keypair.publicKey();
        // // String privateKey = keypair.privateKey();
        // String publicKeyQ = keypair.publicKeyQ();
        // String privateKeyD = keypair.privateKeyD();
        // // System.out.println("publicKey: " + publicKey);
        // // System.out.println("privateKey: " + privateKey);
        // System.out.println("publicKeyQ: " + publicKeyQ);
        // System.out.println("privateKeyD: " + privateKeyD);
        //
        // System.out.println("------------------------------------------------");
        //
        // // SM2 加密解密
        // String sm2EncryptData = SmCryptoUtils.sm2Encrypt(text, publicKey);
        // System.out.println("encryptData: " + sm2EncryptData);
        // String sm2DecryptData = SmCryptoUtils.sm2Decrypt(sm2EncryptData, privateKey);
        // System.out.println("decryptData: " + sm2DecryptData);
        // System.out.println("------------------------------------------------");
        //
        // // SM2 签名验签
        // String sigValueHex = SmCryptoUtils.sm2Signature(text, privateKey);
        // System.out.println("sigValueHex: " + sigValueHex);
        // boolean verifyResult = SmCryptoUtils.sm2VerifySignature(text, sigValueHex, publicKey);
        // System.out.println("verifyResult: " + verifyResult);


        var text = "_测_fasde-2023@试_";
        var key = "c2b34265b8f7c7908685eecda77e825f";
        var iv = "d534564cdd62927fc78205c6b12b4291";
        var publicKey = "047fea744a02dd5146800f8170a5703066cfe5cd746da3e0f1f12941040299d512023362574282d592aa470d93ecedab6152c27ef52d0b44824084321e413f5fbd";
        var privateKey = "15e95cef57521b8181f3e6e1f238dcb9378dec65c5467a95198e39724e6f66bc";

        System.out.println("原始数据: " + text);
        System.out.println("key: " + key);
        System.out.println("iv: " + iv);
        System.out.println("publicKey: " + publicKey);
        System.out.println("privateKey: " + privateKey);

        System.out.println("sm3Hash: " + SmCryptoUtils.sm3Hash(text));
        System.out.println("------------------------------------------------");

        var encryptData = SmCryptoUtils.sm4Encrypt(text, key, iv);
        System.out.println("sm4Encrypt: " + encryptData);
        System.out.println("sm4Decrypt: " + SmCryptoUtils.sm4Decrypt(encryptData, key, iv));
        System.out.println("------------------------------------------------");

        // SM2 加密解密
        String sm2EncryptData = SmCryptoUtils.sm2Encrypt(text, publicKey);
        System.out.println("encryptData: " + sm2EncryptData);
        System.out.println("decryptData: " + SmCryptoUtils.sm2Decrypt(sm2EncryptData, privateKey));
        System.out.println("------------------------------------------------");

        // SM2 签名验签
        String sigValueHex = SmCryptoUtils.sm2Signature(text, privateKey);
        System.out.println("sigValueHex: " + sigValueHex);
        System.out.println("verifyResult: " + SmCryptoUtils.sm2VerifySignature(text, sigValueHex, publicKey));

        // SM2 sm2 = new SM2();
        // System.out.println("privateKey: " + HexUtil.encodeHexStr(sm2.getPrivateKey().getEncoded()));
        // System.out.println("publicKey: " + HexUtil.encodeHexStr(sm2.getPublicKey().getEncoded()));
        // // 生成私钥 D，以D值做为js端的解密私钥
        // String privateKeyD = HexUtil.encodeHexStr(BCUtil.encodeECPrivateKey(sm2.getPrivateKey()));
        // System.out.println("私钥D:"+ privateKeyD);
        // // 生成公钥 Q，以Q值做为js端的加密公钥
        // String publicKeyQ = HexUtil.encodeHexStr(((BCECPublicKey) sm2.getPublicKey()).getQ().getEncoded(false));
        // System.out.println("公钥Q:"+ publicKeyQ);

        System.out.println("------------------------------------------------");
        sigValueHex="304402207274649612f22cf1ffc45764e80180959497608c182571f7f43a94b866eb042802202918b92b8da2b49f0e5feaa166f2874ab5aab05df2423beb4f491a71122cea4d";
        System.out.println("手动验签verifyResult: " + SmCryptoUtils.sm2VerifySignature(text, sigValueHex, publicKey));

        System.out.println("------------------------------------------------");
        var encryptData1 ="322014323885f058acfc84d5c2c200bf250435581b74b338b4c81bc9e1f89a02d19ff31b1436dd1882fde3b80d732384a1f0f45cb493d4c6a16cfda36281e1e22b91c80640093136a6340bb95160315ba70e2637b13a3f461ad3165afb461301b31e7322a56446ffc335d392695a21980d1d4c50";
        System.out.println("手动decryptData: " + SmCryptoUtils.sm2Decrypt(encryptData1, privateKey));
    }
}
